package com.mt4.trade.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.json.JSONException;

import com.mt4.trade.utils.TradeUtils;

/**
 * 交易
 * 
 * @author ivan
 * 
 */
public class TradeAction extends AbstractBaseActionSupport {

	public static Long version = 0L;

	public String index() {
		return SUCCESS;
	}

	/**
	 * 发送订单
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public String orderSend() throws JSONException, IOException {
		String rst = "ok";
		// 如果订单未提交成功，则不执行
		if (tradeService.getLocalUserSendOrder(getCurUser()) != null
				&& tradeService.getLocalUserSendOrder(getCurUser()).size() > 0) {
			rst = "error:还有订单未提交成功，请稍候！";
			return jsonResult(rst);
		}
		try {
			String symbol = this.pm.get("symbol");
			String type = this.pm.get("type");
			String magic = this.pm.get("magic");
			tradeService.exOrderSend(getCurUser(), symbol, type, magic);
		} catch (Throwable t) {
			rst = "error:程序错误" + t.toString();
		}
		return jsonResult(rst);
	}

	/**
	 * 获取帐户信息
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public String getAccountInfo() throws JSONException, IOException {
		return jsonResult(TradeUtils.getMTClientFileInfoByUser(getCurUser(),
				TradeUtils.ACCOUNT_FILE_PATH));
	}

	/**
	 * 清空本地正在发送的订单信息
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public String clearSendOrders() throws JSONException, IOException {
		tradeService.clearLocalSendOrder(getCurUser());
		return jsonResult("");
	}

	/**
	 * 对应MT4 start方法
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public String start() throws JSONException, IOException {
		Map<String, Object> rst = new HashMap<String, Object>();
		// 获取货币信息列表
		rst.put("symbolInfo", tradeService.getSymbolInfo(getCurUser()));// EURUSD,1.32250000,1.32270000;GBPUSD,1.61570000,1.61600000;USDJPY,85.90000000,85.92000000;USDCAD,0.99460000,0.99500000;
		// 获取订单列表
		rst.put("orderList", tradeService.getUserOrderList(getCurUser()));
		// 检查订单提交结果
		if (tradeService.checkSendOrder(getCurUser())) {
			// 订单提交成功
			rst.put("commandStatus", "true");
		} else {
			// 订单提交失败
			rst.put("commandStatus", "false");
		}
		// 获取本地订单列表信息
		rst.put("sendOrders", tradeService.getLocalUserSendOrder(getCurUser()));

		// 如果有关闭单，则计算资产、清空关闭单缓存和文件内容
		if (tradeService.checkCloseOrder(getCurUser())) {
			rst.put("isCloseOrder", true);
		} else {
			rst.put("isCloseOrder", false);
		}
		synchronized (version) {
			version++;
			rst.put("v", version);
			return jsonResult(rst);
		}
	}

	/**
	 * 获取货币信息
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public String getSymbolInfoList() throws JSONException, IOException {
		return jsonResult(TradeUtils.getMTClientFileInfoByUser(getCurUser(),
				TradeUtils.SYMBOL_INFO_FILE_PATH));
	}

	/**
	 * 获取订单历史
	 * 
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public String getOrderHistory() throws JSONException, IOException {
		return jsonResult(TradeUtils.getMTClientFileInfoByUser(getCurUser(),
				TradeUtils.ORDER_HISTORY));
	}

}
